package com.etc.controller;

import com.alibaba.fastjson.JSON;
import com.etc.entity.Books;
import com.etc.service.BooksService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.print.Book;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "商品管理相关接口")
@RequestMapping("/books")
@CrossOrigin
public class BooksController {
    @Autowired
    BooksService service;

    /**
     * 模糊查询
     * @param bName 商品名称关键字
     * @return 查询到的商品信息
     */
    @RequestMapping("/get")
    @ApiOperation("商品查询")
    public PageInfo<Books> getBooks(@RequestParam String bName,
                                @RequestParam(defaultValue = "1")int pageNum,
                                @RequestParam(defaultValue = "3")int size){
        PageInfo<Books> pageInfo = service.selectByPrimaryKey(bName, pageNum, size);
        return pageInfo;
    }

    /**
     * 新增商品
     * @param books 商品信息
     * @return true 增加成功 false 增加失败
     */
    @RequestMapping("/insert")
    @ApiOperation("商品增加")
    public Map<String,Object> insertBook(@RequestParam String books,
                                         @RequestParam MultipartFile file){
        Map<String, Object> map = new HashMap<>();
        Books book = JSON.parseObject(books, Books.class);
        String name = file.getOriginalFilename();
        book.setbImg(name);
        //定义文件存储位置
        File files = new File("E:\\uploadFile\\" + name);
        //写入
        try {
            file.transferTo(files);
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean b = service.insert(book);
        if (b) {
            map.put("msg","success");
        }else {
            map.put("msg","fail");
        }
        return map;
    }

    /**
     * 修改商品
     * @param
     * @return true 修改成功 false 修改失败
     */
    @RequestMapping("/update")
    @ApiOperation("商品修改")
    public PageInfo<Books> updateBook(@RequestParam String book1,
                                      @RequestParam(defaultValue = "1")int pageNum,
                                      @RequestParam(defaultValue = "3")int size){
        Books book = JSON.parseObject(book1, Books.class);
        PageInfo<Books> pageInfo = service.updateByPrimaryKey(book, pageNum, size);
        return pageInfo;
    }

    /**
     * 删除商品
     * @param bId 商品id
     * @return true 删除成功 false 删除失败
     */
    @RequestMapping("delete")
    @ApiOperation("商品删除")
    public PageInfo<Books> deleteBook(Integer bId,
                              String bName,
                              @RequestParam(defaultValue = "1")int pageNum,
                              @RequestParam(defaultValue = "3")int size){
        boolean b = service.deleteByPrimaryKey(bId);
        PageInfo<Books> pageInfo = getBooks(bName, pageNum, size);
        return pageInfo;
    }
}
